package com.jp.knowledge.my.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.adapter.OrderRelationAdapter;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.model.OrderModel;
import com.jp.knowledge.util.SpanUtils;
import com.jp.knowledge.view.JpRecycleView;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/5/4.
 */

public class OrderAdapter extends RecycleVAdapter<OrderModel> implements View.OnClickListener {

    private static final int BUTTON_STATE_ORDER_PAY = 0x01;         // 支付订单
    private static final int BUTTON_STATE_ORDER_CANCEL = 0x02;      // 取消订单
    private static final int BUTTON_STATE_ORDER_REFUNDS = 0x03;     // 退款
    private static final int BUTTON_STATE_ORDER_RECEIPT = 0x04;     // 确认收货

    private SimpleDateFormat dateFormat;

    public OrderAdapter(Context context, @NonNull List<OrderModel> list) {
        super(context, list);
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_order;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        OrderModel data = getData(position);
        holder.setTextView(R.id.orderId, "订单号：" + data.getOrderId());
        holder.setTextView(R.id.orderTitle, data.getOrderTitle());
        holder.setTextView(R.id.orderTime, dateFormat.format(data.getOrderTime()));
        holder.setTextView(R.id.orderAmount, String.format("已付款：￥%.2f", ((float)data.getOrderAmount())/100));

        if(data.getRelationList() == null || data.getRelationList().size() == 0){
            holder.get(R.id.orderRelation).setVisibility(View.GONE);
        } else if(data.getRelationList().size() == 1){
            holder.get(R.id.orderRelation).setVisibility(View.VISIBLE);
            holder.get(R.id.orderRelationMultiView).setVisibility(View.GONE);
            holder.get(R.id.orderRelationSingleView).setVisibility(View.VISIBLE);
            OrderModel.RelationListBean relationListBean = data.getRelationList().get(0);
            holder.setImageUrl(R.id.orderRelationSingleIcon, relationListBean.getIcon());
            TextView relationTitle = holder.getTextView(R.id.orderRelationSingleTitle);
            TextView relationDesc = holder.getTextView(R.id.orderRelationSingleDesc);
            if(relationListBean.getName() == null || relationListBean.getName().trim().length() == 0){
                relationTitle.setVisibility(View.GONE);
            } else{
                relationTitle.setVisibility(View.VISIBLE);
                relationTitle.setText(relationListBean.getName());
            }
            if(relationListBean.getCompanyName() == null || relationListBean.getCompanyName().trim().length() == 0){
                relationDesc.setVisibility(View.GONE);
            } else{
                relationDesc.setVisibility(View.VISIBLE);
                relationDesc.setText(relationListBean.getCompanyName());
            }
        } else{
            holder.get(R.id.orderRelation).setVisibility(View.VISIBLE);
            holder.get(R.id.orderRelationSingleView).setVisibility(View.GONE);
            JpRecycleView recyclerView = holder.get(R.id.orderRelationMultiView);
            recyclerView.setVisibility(View.VISIBLE);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
            recyclerView.setAdapter(new OrderRelationAdapter(mContext, data.getRelationList()));
        }

        int orderState = data.getOrderState();
        int payState = data.getPayState();
        TextView orderDesc = holder.getTextView(R.id.orderDesc);
        orderDesc.setText(getOrderDestText(orderState, payState));

        TextView btn1 = holder.getTextView(R.id.orderBtn1);
        TextView btn2 = holder.getTextView(R.id.orderBtn2);
        btn1.setTag(-1);
        btn2.setTag(-1);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        View orderBtnView = holder.get(R.id.orderBtnView);
        orderBtnView.setVisibility(View.VISIBLE);
        if((orderState == 1) && (payState == 0 || payState == 2)){
            btn1.setVisibility(View.VISIBLE);
            btn1.setText("去支付");
            btn1.setTag(position * 10 + BUTTON_STATE_ORDER_PAY);
            btn2.setVisibility(View.VISIBLE);
            btn2.setText("取消订单");
            btn2.setTag(position * 10 + BUTTON_STATE_ORDER_CANCEL);
        } else if(orderState == 2 && payState == 1){
            btn1.setVisibility(View.VISIBLE);
            btn1.setText("确认收货");
            btn1.setTag(position * 10 + BUTTON_STATE_ORDER_RECEIPT);
            btn2.setVisibility(View.GONE);
        } else if((orderState == 4 || orderState == 5) && (payState == 1)){
            btn1.setVisibility(View.VISIBLE);
            btn1.setText("取消订单");
            btn1.setTag(position * 10 + BUTTON_STATE_ORDER_CANCEL);
            btn2.setVisibility(View.GONE);
        } else if(orderState == 3 && payState == 1){
            btn1.setVisibility(View.VISIBLE);
            btn1.setText("申请退款");
            btn1.setTag(position * 10 + BUTTON_STATE_ORDER_REFUNDS);
            btn2.setVisibility(View.GONE);
        } else{
            orderBtnView.setVisibility(View.GONE);
        }
        btn1.setVisibility(View.GONE);
        if(btn2.getVisibility() != View.VISIBLE)
            orderBtnView.setVisibility(View.GONE);
    }

    private SpannableStringBuilder getOrderDestText(int orderState, int payState){
        SpanUtils spanUtils = new SpanUtils(mContext);
        if(orderState == 1)
            spanUtils.append("待付款").setForegroundColor(mContext.getResources().getColor(R.color.google_red));
        else if(orderState == 2)
            spanUtils.append("待完成").setForegroundColor(mContext.getResources().getColor(R.color.google_red));
        else if(orderState == 3)
            spanUtils.append("已取消").setForegroundColor(mContext.getResources().getColor(R.color.font_black_think));
        else if(orderState == 4)
            spanUtils.append("待收货").setForegroundColor(mContext.getResources().getColor(R.color.google_red));
        else if(orderState == 5)
            spanUtils.append("已完成").setForegroundColor(mContext.getResources().getColor(R.color.font_black_think));
        else if(orderState == 6)
            spanUtils.append("售后中").setForegroundColor(mContext.getResources().getColor(R.color.google_red));
        if(payState == 0)
            spanUtils.append(",未支付").setForegroundColor(mContext.getResources().getColor(R.color.font_black_think));
        else if(payState == 1)
            spanUtils.append(",已支付").setForegroundColor(mContext.getResources().getColor(R.color.font_black_think));
        else if(payState == 2)
            spanUtils.append(",支付失败").setForegroundColor(mContext.getResources().getColor(R.color.font_black_think));
        else if(payState == 3)
            spanUtils.append(",已退款").setForegroundColor(mContext.getResources().getColor(R.color.font_black_think));
        return spanUtils.create();
    }

    @Override
    public void onClick(View v) {
        if(orderBtnClickListener == null)
            return;
        int buttonState = (int)v.getTag();
        switch (buttonState%10){
            case BUTTON_STATE_ORDER_PAY:
                orderBtnClickListener.onOrderPay(buttonState/10);
                break;
            case BUTTON_STATE_ORDER_CANCEL:
                orderBtnClickListener.onOrderCancel(buttonState/10);
                break;
            case BUTTON_STATE_ORDER_REFUNDS:
                orderBtnClickListener.onOrderRefunds(buttonState/10);
                break;
            case BUTTON_STATE_ORDER_RECEIPT:
                orderBtnClickListener.onOrderReceipt(buttonState/10);
                break;
        }
    }

    private OrderBtnClickListener orderBtnClickListener;
    public void setOrderBtnClickListener(OrderBtnClickListener orderBtnClickListener){
        this.orderBtnClickListener = orderBtnClickListener;
    }
    public interface OrderBtnClickListener{
        void onOrderPay(int position);
        void onOrderCancel(int position);
        void onOrderRefunds(int position);
        void onOrderReceipt(int position);
    }
}
